09. Exercise: DAO - SleepDatabaseDao

L6 14 DAO SleepDatabaseDao Code SC

Update:
All the functions declared inside the SleepDatabaseDao interface should be declared as suspend, except the one which returns a LiveData. The feature of a suspend function is that it can suspend the execution (pause and resume at a later time) of a coroutine.

Now it’s your turn to complete this exercise yourself.

In this step, you'll create queries for inserting, requesting,, and updating sleep data.

  1. Create an interface SleepDatabaseDao and annotate it with @Dao:
 @Dao
interface SleepDatabaseDao {}
  1. Add an @Insert annotation, and an insert() function that takes one SleepNight.

    That's it. Room will generate all the necessary code to insert the passed-in SleepNight into the database. Note that you can call the function anything you want:

   @Insert
   suspend fun insert(night: SleepNight)
  1. In the same way, add an @Update annotation with an update() function for one SleepNight:
   @Update
   suspend fun update(night: SleepNight)
  1. Add a @Query annotation with a function get() that takes a Long key argument and returns a nullable SleepNight:
   @Query
   suspend fun get(key: Long): SleepNight?
  1. Add a parameter to @Query.

    Make it a String that is a SQLite query that selects all columns from the daily_sleep_quality_table WHERE the nightId matches the :key argument.

 @Query("SELECT * from daily_sleep_quality_table WHERE nightId = :key")
  suspend fun get(key: Long): SleepNight?
  1. Add another @Query with a clear() function and a SQLite query to delete everything from the daily_sleep_quality_table:
   @Query("DELETE FROM daily_sleep_quality_table")
   suspend fun clear()
  1. **Add a @Query to getAllNights(). **

    The SQLite query should return all columns from the daily_sleep_quality_table, ordered in descending order. Let getAllNights() return a list of SleepNight as LiveData. Room keeps this LiveData updated for us, and we don't have to specify an observer for it.

   @Query("SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC")
   fun getAllNights(): LiveData<List<SleepNight>>
  1. Add a @Query to getTonight(). Make the returned SleepNight nullable, so that it can handle if the table is empty.

    You get tonight by writing a SQLite query that returns the first element of a list of results ordered by nightId in descending order. Use LIMIT 1 to return only one element.

 @Query("SELECT * FROM daily_sleep_quality_table ORDER BY nightId DESC LIMIT 1")
suspend fun getTonight(): SleepNight?
  1. Run your app to make sure it has no errors.

If you want to start at this step, you can download this exercise from: Step.02-Exercise-Create-SleepDatabaseDao.

You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.

Once you’re done, you can check your solution against the solution we’ve provided here: Step.02-Solution-Create-SleepDatabaseDao, or using this git diff.

Task Description:

Complete the following tasks to create a SleepDatabase DAO.

Task List:

Task Feedback:

Great, you have defined how your app is going to interact with the database!

Reference Documentation